"""
数据访问层模块
提供Repository模式的数据访问接口
"""

from .database import (
    DatabaseManager,
    db_manager,
    DatabaseOperation,
    get_db,
    transactional,
    get_current_db_session,
    ConnectionPoolManager,
    pool_manager,
    init_database,
    cleanup_database
)
from .base import (
    BaseRepository,
    CacheableRepository
)
from .audiobook import (
    AudioBookRepository,
    AudioBookChapterRepository,
    AudioBookProgressRepository,
    AudioBookReviewRepository,
    AudioBookTagRepository
)
from .validation import (
    ValidationEngine,
    validation_engine,
    ValidationRule,
    Validators,
    TransactionManager,
    TransactionIsolation,
    DataValidator,
    BulkOperationManager,
    DataIntegrityChecker,
    validate_data
)

__all__ = [
    # 数据库管理
    'DatabaseManager',
    'db_manager',
    'DatabaseOperation',
    'get_db',
    'transactional',
    'get_current_db_session',
    'ConnectionPoolManager',
    'pool_manager',
    'init_database',
    'cleanup_database',
    
    # Repository基础
    'BaseRepository',
    'CacheableRepository',
    
    # 有声读物Repository
    'AudioBookRepository',
    'AudioBookChapterRepository',
    'AudioBookProgressRepository',
    'AudioBookReviewRepository',
    'AudioBookTagRepository',
    
    # 验证和事务
    'ValidationEngine',
    'validation_engine',
    'ValidationRule',
    'Validators',
    'TransactionManager',
    'TransactionIsolation',
    'DataValidator',
    'BulkOperationManager',
    'DataIntegrityChecker',
    'validate_data'
]

# 版本信息
__version__ = '1.0.0'
__author__ = 'Offline Language Player Team'


def get_repository_classes():
    """获取所有Repository类"""
    return {
        'audiobook': AudioBookRepository,
        'audiobook_chapter': AudioBookChapterRepository,
        'audiobook_progress': AudioBookProgressRepository,
        'audiobook_review': AudioBookReviewRepository,
        'audiobook_tag': AudioBookTagRepository
    }


def create_repository(model_name: str, session=None):
    """根据模型名创建Repository实例"""
    repository_classes = get_repository_classes()
    
    if model_name in repository_classes:
        return repository_classes[model_name](session)
    else:
        raise ValueError(f"Unknown repository for model: {model_name}")


# 便捷函数
def get_audiobook_repository(session=None):
    """获取有声读物Repository"""
    return AudioBookRepository(session)


def get_chapter_repository(session=None):
    """获取章节Repository"""
    return AudioBookChapterRepository(session)


def get_progress_repository(session=None):
    """获取进度Repository"""
    return AudioBookProgressRepository(session)


def get_review_repository(session=None):
    """获取评论Repository"""
    return AudioBookReviewRepository(session)


def get_tag_repository(session=None):
    """获取标签Repository"""
    return AudioBookTagRepository(session)
